import VirtualItem from "../../ui/VirtualItem";
import { VirtualArgs } from "../../ui/VirtualList";
import DataUtil from "../../util/DataUtil";
import { TableCfgItem, TableClickEvent } from "./TableLayout";

const {ccclass, property} = cc._decorator;

export interface TableRowArgs extends VirtualArgs {
    ts_code: string,
    rowData: any, 
    cfgs: TableCfgItem[],
    clickEvent: TableClickEvent,
    uniqueKey: string,
}

export class TableRowEvent { 
    uniqueKey: string = '';
    eventKey: string = '';
    param: any = null;
    object: TableRowItem;
}

@ccclass
export default class TableRowItem extends VirtualItem<TableRowArgs> {

    @property(cc.Node)
    cellItem: cc.Node = null;
    @property(cc.Boolean)
    isTitle = false;

    private items: cc.Node[] = [];
    private _cfgs: TableCfgItem[] = null;
    private _clickEvent: TableClickEvent = null;
    private _rowData: any = null;
    private _uniqueKey: string = '';
    onLoad () {
        cc.director.on('TableRowClickEvent', this.onTableRowClickEvent, this);
    }

    protected onDestroy(): void {
        cc.director.off('TableRowClickEvent', this.onTableRowClickEvent, this);
    }

    public onRefresh(args: TableRowArgs) {
        this.isTitle = false;
        this._cfgs = args.cfgs;
        this._clickEvent = args.clickEvent;
        this._rowData = args.rowData;
        this._uniqueKey = args.uniqueKey;
        this.initItems();
        for (let index = 0; index < this._cfgs.length; index++) {
            const cfg = this._cfgs[index];
            const node = this.items[index];
            this.showSelect(false);
            if (cfg.dataType == "number") {
                node.getComponentInChildren(cc.Label).string = DataUtil.formatNumberStr(args.rowData[cfg.key]);
            } else if (cfg.dataType == "numberW") {
                node.getComponentInChildren(cc.Label).string = DataUtil.formatNumberWStr(args.rowData[cfg.key]);
            } else if (cfg.dataType == "string") {
                node.getComponentInChildren(cc.Label).string = "" + args.rowData[cfg.key];
            } else if (cfg.dataType == "integer") {
                node.getComponentInChildren(cc.Label).string = "" + args.rowData[cfg.key];
            } else if (cfg.dataType == "numberB") {
                node.getComponentInChildren(cc.Label).string = DataUtil.formatNumberBStr(args.rowData[cfg.key]);
            }
        }
    }

    public showTableCfg(cfgs: TableCfgItem[], uniqueKey: string) {
        this.isTitle = true;
        this._cfgs = cfgs;
        this._uniqueKey = uniqueKey;
        this.initItems();
        for (let index = 0; index < cfgs.length; index++) {
            const node = this.items[index];
            let cfg = cfgs[index];
            node.getComponentInChildren(cc.Label).string = cfg.title;
            node.color = cc.Color.GRAY;
            if (cfg.isSort) {
                let button = node.getComponent(cc.Button);
                let clickHandler = new cc.Component.EventHandler();
                clickHandler.target = this.node;
                clickHandler.component = "TableRowItem";
                clickHandler.handler = "onClickTitle";
                clickHandler.customEventData = cfg.key;
                button.clickEvents.push(clickHandler);
            }
        }
    }

    public showSelect (isSelect: boolean) {
        if (this.isTitle) return;
        for (let index = 0; index < this.items.length; index++) {
            const item = this.items[index];
            item.color = isSelect ? cc.Color.RED : cc.Color.WHITE;
        }
    }

    public initItems() {
        if (this.items.length != 0) return;
        this.items[0] = this.cellItem;
        this.items[0].width = this._cfgs[0].witdh;
        for (let index = 1; index < this._cfgs.length; index++) {
            const cfg = this._cfgs[index];
            let node = cc.instantiate(this.cellItem);
            this.items.push(node);
            node.width = cfg.witdh;
            this.node.addChild(node);
        }
        this.getComponent(cc.Layout).updateLayout();
    }

    private onTableRowClickEvent (event: TableRowEvent) {
        if (this._uniqueKey == event.uniqueKey && event.object != this) {
            this.showSelect(false);
        }
    }
    onClick () {
        if (this._clickEvent) {
            let param = this._rowData[this._clickEvent.paramKey];
            let event = new TableRowEvent();
            event.uniqueKey = this._uniqueKey;
            event.param = param;
            event.object = this;
            event.eventKey = this._clickEvent.eventKey;
            cc.director.emit("TableRowClickEvent", event);
            this.showSelect(true);
        }
    } 

    onClickTitle (touch, eventKey: string) {
        let event = new TableRowEvent();
        event.uniqueKey = this._uniqueKey;
        event.eventKey = eventKey;
        event.object = this;
        event.param = eventKey;
        cc.director.emit("TableRowRankEvent", event);
    }

    // update (dt) {}
}
